namespace hjc {

    export class ThemeAdapter implements eui.IThemeAdapter {

        public getTheme(url: string, onSuccess: Function, onError: Function, context: any): void {

            function onResError(e: RES.ResourceEvent): void {
                if (e.resItem.url == url) {
                    RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, onResError, null);
                    utils.call(onError, context)
                }
            }

            if (isDefined('generateEUI')) egret.callLater(() => utils.call(onSuccess, generateEUI), this);
            else if (isDefined('generateEUI2')) {
                RES.getResByUrl("resource/gameEui.json", (data, url) => {
                    window["JSONParseClass"]["setData"](data);
                    egret.callLater(() => utils.call(onSuccess, generateEUI2), this);
                }, this, RES.ResourceItem.TYPE_JSON);
            } else if (isDefined('generateJSON')) {
                if (url.indexOf(".exml") > -1) {
                    let dataPath = url.split("/");
                    dataPath.pop();
                    let dirPath = dataPath.join("/") + "_EUI.json";
                    if (!generateJSON.paths[url]) {
                        RES.getResByUrl(dirPath, data => {
                            window["JSONParseClass"]["setData"](data);
                            egret.callLater(() => utils.call(onSuccess, context, generateJSON.paths[url]), this);
                        }, this, RES.ResourceItem.TYPE_JSON);
                    } else egret.callLater(() => utils.call(onSuccess, context, generateJSON.paths[url]), this);
                } else egret.callLater(() => utils.call(onSuccess, context, generateJSON), this);
            } else {
                RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, onResError, null);
                RES.getResByUrl(url, (e: string) => utils.call(onSuccess, context, e), this, RES.ResourceItem.TYPE_TEXT);
            }
        }
    }
}

declare let generateEUI: { paths: string[], skins: any }
declare let generateEUI2: { paths: string[], skins: any }
declare let generateJSON: { paths: string[], skins: any }